package ph.com.gs3.chat.managers;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import ph.com.gs3.chat.R;
import ph.com.gs3.chat.utilities.FLLogger;
import ph.com.gs3.imagemanager.ImageLoader;
import android.content.Context;
import android.widget.ImageView;

/**
 * 
 * @author Ervinne Sodusta
 * 
 */
public class ImageManager {

	public static final String TAG = ImageManager.class.getSimpleName();

	public static final String IMAGE_CACHE_PATH = ".formalistics/img_cache";

	public ImageLoader imageLoader;

	private Map<String, List<ImageView>> queuedUserImageViews;
	private Map<String, String> downloadedImages;

	private int imageNotAvailableStub = R.drawable.user_avatar_empty;

	private static ImageManager defaultInstance;

	public static ImageManager getDefaultInstance(Context context) {

		if (defaultInstance == null) {
			defaultInstance = new ImageManager(context);
		}

		return defaultInstance;

	}

	public ImageManager(Context context) {

		imageLoader = new ImageLoader(context, IMAGE_CACHE_PATH);
		// imageLoader.setDefaultServer(server);
		imageLoader.setImageNotAvailableResource(imageNotAvailableStub);

		queuedUserImageViews = new HashMap<String, List<ImageView>>();
		downloadedImages = new HashMap<String, String>();

	}

	public void requestUserImage(String userWebId, String imageURL, ImageView imageView) {

		if (userWebId == null || imageURL == null) {
			FLLogger.d(TAG, "Requested user image but the user web id or image url is not provided");
			return;
		}

		// check if the image has be fetched already.
		if (downloadedImages.containsKey(userWebId)) {
			// the image is existing, simply load the image to the image view
			imageLoader.displayImage(downloadedImages.get(userWebId), imageView);
		} else {

			imageView.setImageResource(imageLoader.getImageNotAvailableResource());

			if (queuedUserImageViews.containsKey(userWebId)) {
				// register the image view to an existing list
				queuedUserImageViews.get(userWebId).add(imageView);
			} else {
				// create a new list for this user and request for the user's data
				List<ImageView> userImageViewQueue = new ArrayList<ImageView>();
				userImageViewQueue.add(imageView);
				queuedUserImageViews.put(userWebId, userImageViewQueue);

				// TODO: check if there is a need for re checking connection here

				downloadImage(userWebId, imageURL);

			}
		}

	}

	public void downloadImage(String userWebId, String imageURL) {

		if (!queuedUserImageViews.containsKey(userWebId)) {
			// The image view list was suddenly lost, this may be caused
			// by changing activities etc., ignore and abort operation
			return;
		}

		// load the image to each image views of the corresponding users
		List<ImageView> userImageViews = queuedUserImageViews.get(userWebId);

		if (imageURL != null && !imageURL.isEmpty()
		        && !"null".equals(imageURL.toLowerCase(Locale.ENGLISH))) {

			for (ImageView currentUserImageView : userImageViews) {
				imageLoader.displayImage(imageURL, currentUserImageView);
			}

			// images that are already downloaded should have their
			// corresponding image views removed from the queue, a reference
			// to the image will now be stored in the "downloadedImages" map
			// and will be used by other image views instead of downloading
			// the image again.
			queuedUserImageViews.remove(userWebId);
			downloadedImages.put(userWebId, imageURL);

		} else {
			for (ImageView currentUserImageView : userImageViews) {
				currentUserImageView.setImageResource(imageLoader.getImageNotAvailableResource());
			}
		}

	}

}
